package io.renren.modules.hos.service.impl;

import io.renren.common.utils.CodeUtils;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.SendMsgUtils;
import io.renren.modules.hos.service.CodeService;
import io.renren.modules.sys.dao.SysCaptchaDao;
import io.renren.modules.sys.entity.SysCaptchaEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private SysCaptchaDao captchaDao;

    @Override
    public Map<String, Object> sendMsg(String phone) {

        String code = CodeUtils.getCode();
        Map<String, String> codeMap = new HashMap<>(2);
        codeMap.put("code", code);

        Boolean flag = SendMsgUtils.sendMsg(phone, code);
        Map<String, Object> map = new HashMap<>(2);
        String uuid = UUID.randomUUID().toString();

        if (flag){
            SysCaptchaEntity captchaEntity = new SysCaptchaEntity();
            captchaEntity.setUuid(uuid);
            captchaEntity.setCode(code);
            //5分钟后过期
            captchaEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), 1));
            captchaDao.insert(captchaEntity);
            map.put("uuid", uuid);
        }

        map.put("flag", flag);
        System.out.println(code);
        return map;
    }
}
